/*
 * LDIR
 * 
 * LDIR, a GPS cartography application built for Let's Do It Romania eco 
 * campaign.
 * Copyright (C) 2010 Genera Software <office@generasoftware.com>
 * 
 * http://generasoftware.com/
 * 
 * This program is free software; you can redistribute it and/or modify it under
 * the terms of the GNU General Public License as published by the Free Software
 * Foundation; either version 2 of the License, or (at your option) any later
 * version.
 * 
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
 * details.
 * 
 * You should have received a copy of the GNU General Public License along with
 * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
 * Place, Suite 330, Boston, MA 02111-1307 USA
 */
#ifndef JSONIMPL_H
#define JSONIMPL_H

// INCLUDES
// System Includes
#include <e32base.h>  // CBase, link against euser.lib
#include <stringpool.h>

#include "yajl_parse.h"
#include "yajl_gen.h"

class CJsonParserImpl : public CBase
{
public:
	CJsonParserImpl(TBool aAllowComments, TBool aCheckUTF8);
	~CJsonParserImpl();
	TVersion Version() const;
	TInt ParseL(const TDesC8& aData);
	// Might leave because we allocate the parser here
	void SetContentHandlerL(MJsonContentHandler* aContenthandler);
private:
	static int yajl_null(void* ctx);
	static int yajl_boolean(void * ctx, int boolVal);
	static int yajl_integer(void * ctx, long integerVal);
	static int yajl_double(void * ctx, double doubleVal);
	/** A callback which passes the string representation of the number
	*  back to the client.  Will be used for all numbers when present */
	static int yajl_number(void * ctx, const char * numberVal,
		unsigned int numberLen);

	/** strings are returned as pointers into the JSON text when,
	* possible, as a result, they are _not_ null padded */
	static int yajl_string(void * ctx, const unsigned char * stringVal,
		unsigned int stringLen);

	static int yajl_start_map(void * ctx);
	static int yajl_map_key(void * ctx, const unsigned char * key,
		unsigned int stringLen);
	static int yajl_end_map(void * ctx);        

	static int yajl_start_array(void * ctx);
	static int yajl_end_array(void * ctx);        

	yajl_callbacks iCallbacks;

private: // data
	yajl_parser_config iConfig;
	yajl_handle iParserHandle;
	MJsonContentHandler* iContentHandler;
};

class CJsonGeneratorImpl : public CBase
{
public: 
	CJsonGeneratorImpl(const TStringTable& aDictionary, TBool aBeautify, const TDesC8& aIndent);
	~CJsonGeneratorImpl();
	TVersion Version() const;
	void ConstructL();
public:
	TInt GenerateL(CEntity* iNode,
		const unsigned char ** buf,
		unsigned int * len);
	void Free();
	TInt Generate(CEntity* iNode);
	TInt GenerateChildren(CEntity* iNode);
private:
	yajl_gen_config iConfig;
	yajl_gen iGenHandle;
	TBuf8<10> iIndent;
	const TStringTable& iDictionaryTable;
	RStringPool iDictionary;
};


#endif // JSONIMPL_H

// End of File
